package com.thread.exe;

import java.util.concurrent.CountDownLatch;

//测试安全带
public class TestHarness {
	
	public long timeTasks(int nThread, final Runnable task) throws InterruptedException {
		final CountDownLatch start = new CountDownLatch(1);
		final CountDownLatch end = new CountDownLatch(nThread);
		
		for (int i = 0; i < nThread; i++) {
			Thread t = new Thread() {
				@Override
				public void run() {
					try {
						start.await();
					} catch (InterruptedException e) {
						e.printStackTrace();
					} finally {
						end.countDown();
					}
				}
			};
			t.start();
		}
		
		long startTime = System.currentTimeMillis();
		start.countDown();
		end.await();
		long endTime = System.currentTimeMillis();
		return endTime - startTime;
	}
	
	public static RuntimeException launderThrowable(Throwable t) {
		if(t instanceof RuntimeException)
			return (RuntimeException) t;
		else if(t instanceof Error)
			throw (Error) t;
		else
			throw new IllegalStateException("Not unchected", t);
	}
}
